﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LDTB.Service;

namespace LDTB
{
    public partial class WardManager : Form
    {
        LDTBDataDataContext ds = DataService.DB;

        public WardManager()
        {
            InitializeComponent();
        }

        private void WardManager_Load(object sender, EventArgs e)
        {


            FillDataToGrid();

        }

        public void FillDataToGrid()
        {

            var wards = from w in ds.Wards
                        select w;
            this.dataGridView1.DataSource = wards;


        }


        private void button1_Click(object sender, EventArgs e)
        {
            AddWard aw = new AddWard();
            aw.ShowDialog();
            aw.Close();
            aw.Dispose();

            FillDataToGrid();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int id=int.Parse(this.dataGridView1.CurrentRow.Cells[0].Value.ToString());
            EditWard ew = new EditWard(id);
            ew.ShowDialog();
            ew.Close();
            ew.Dispose();

            FillDataToGrid();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //xoa du lieu

            int id = int.Parse(this.dataGridView1.CurrentRow.Cells[0].Value.ToString());
             
            // hien thong bao 

            DialogResult dr = MessageBox.Show(this,
                                              "Nếu xóa thì các xã thuộc huyện này  và danh sách người tàn tật thuộc huyện này sẽ bị xóa <br/> bạn có chắc chắn xóa không ? ",
                                              "Xóa Huyện ", MessageBoxButtons.YesNo);

            if(dr==DialogResult.Yes)
            {
                var people = from p in ds.Peoples
                             join ward in ds.Villages on p.LocationID equals ward.VillageID
                             where ward.WardID == id
                             select p;
                
                ds.Peoples.DeleteAllOnSubmit(people);
                ds.SubmitChanges();


                var village = from vl in ds.Villages
                              where vl.WardID == id 
                              select vl;

                ds.Villages.DeleteAllOnSubmit(village);
                ds.SubmitChanges();

                var ward_ = from wd in ds.Wards
                           where wd.ID == id
                           select wd;

                ds.Wards.DeleteAllOnSubmit(ward_);
                ds.SubmitChanges();


               FillDataToGrid();

            }

        }
    }
}
